<?php
session_start();

//open connection
$ch = curl_init();

$url = sprintf('https://api.sandbox.slcedu.org/api/rest/v1/students/%s?views=assessments,attendances,gradebook,transcript', $_GET['UUID']);

$token = $_SESSION['access_token'];
$code = $_SESSION['code'];

$auth = sprintf('Authorization: bearer %s', $token);

$headers = array(
  'Content-Type: application/vnd.slc+json',
  'Accept: application/vnd.slc+json',
  $auth);

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);

//echo $result;
$student = json_decode($result);

// If response is '401 Unauthorized', redirect back to home page for authentication
if ($student->code == '401') {
  header('Location: index.php');
  die();
}

//get parents for this student
//open connection
$ch = curl_init();

$url = sprintf('https://api.sandbox.slcedu.org/api/rest/v1/students/%s/studentParentAssociations/parents', $_GET['UUID']);

$token = $_SESSION['access_token'];
$code = $_SESSION['code'];

$auth = sprintf('Authorization: bearer %s', $token);

$headers = array(
  'Content-Type: application/vnd.slc+json',
  'Accept: application/vnd.slc+json',
  $auth);

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

//execute post
$parents = curl_exec($ch);
//close connection
curl_close($ch);
$parents = json_decode($parents);
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Snapshot</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="js/bootstrap.min.js"></script>
<div class="container-fluid">
<div class="row-fluid">
<div class="span3">
	<address><strong>
<?php
include 'lib/functions.php';

echo name($student);
echo '</strong> <small>(' . $student->sex . ')</small><br>';
echo $student->gradeLevel;
echo '<br>';
echo '<small>born:</small> ' . $student->birthData->birthDate . ' ' . $student->birthData->cityOfBirth . ' ' . $student->birthData->countryOfBirthCode;
echo '<br>';
echo '<table class="table table-condensed">';
foreach ($student->address as $address) {
	echo '<tr><td>';
	if ($address->addressType == 'Home') { $style=' class="text-success"'; } else { $style=' class="muted"'; }
	if ($address->countryCode == 'US') { $country=''; } else { $country=' ' . $address->countryCode; }
	echo '<font' . $style . '><small>' . $address->addressType . '</small></font></td><td' . $style . '>' . $address->streetNumberName . '<br>' . $address->city . ', ' . $address->stateAbbreviation . ' ' . $address->postalCode . $country . '</td></tr>';
}
echo '</table>';
foreach ($student->electronicMail as $email) {
	echo '<small class="muted">' . $email->emailAddressType . '</small> <a href=mailto:"' . $email->emailAddress . '">' . $email->emailAddress . '</a><br>';
}
foreach ($student->telephone as $phone) {
	if ($phone->primaryTelephoneNumberIndicator) { $style=' class="text-success"'; } else { $style=' class="muted"'; }
	echo '<font' . $style . '><small>' . $phone->telephoneNumberType . '</small> ' . $phone->telephoneNumber . '</font><br>';
}
foreach ($parents as $parent) {
	foreach ($parent->studentParentAssociation as $relation) {
		if ($relation->studentId == $student->id) {
			$relationship = $relation;
		}
	}
	echo '<small class="muted">' . $relationship->relation . '</small> ';
	if ($relationship->emergencyContactStatus) {
		$style = ' class="text-info"';
	} else {
		$style = '';
	}
	if ($relationship->primaryContactStatus) {
		$style = ' class="text-success"';
	}
	$phones = array();
	foreach ($parent->telephone as $phone) {
		array_push($phones, $phone->telephoneNumber);
	}
	if ($phones) {
		echo '<abbr title="' . implode(', ', $phones) . '"' . $style . '>' . name($parent) . '</abbr>';
	} else {
		echo '<font' . $style . '>' . name($parent) . '</font>';		
	}
	echo '<br>';
}
?>
</address>
</div>
<div class="span9">
<div class="row">
<div class="span2">
Demographics 
</div>
<div class="span7">
<?php
if ($student->languages) {
	echo 'Speaks ';
	foreach ($student->languages as $lang) {
		echo $lang->LanguageItemType , ' ';
	} 
}
if ($student->limitedEnglishProficiency <> 'NotLimited') {
	echo  '<font class="text-error">Limited English Proficiency</font>';
}
if ($student->economicDisadvantaged) {
	echo '<br>';
	echo  'Economically Disadvantaged';
}
if ($student->schoolFoodServicesEligibility) {
	if (!($student->economicDisadvantaged)) {
		echo '<br>';
	}
	echo  ' Eligible for Food Services';
}
if ($student->learningStyles) {
	echo '<br>';
	foreach ($student->learningStyles as $learn) {
		echo $learn , ' ';
	} 
}
if ($student->disabilities) {
	echo '<br>Disabilities: ';
	foreach ($student->disabilities as $disablity) {
		echo $disablity->disability , ' ';
	}
} 
if ($student->race) {
	echo '<br>Race(s): ';
	foreach ($student->race as $race) {
		echo $race , ' ';
	}
} 
if ($student->hispanicLatinoEthnicity) {
	echo  ' Hispanic/Latino';
}
if ($student->studentCharacteristics) {
	echo 'Characteristics ';
	foreach ($student->studentCharacteristics as $characteristic) {
		echo $characteristic->characteristic , ' ';
	} 
}
if ($student->studentIndicators) {
	echo 'Indicators ';
	foreach ($student->studentIndicators as $indicator) {
		echo $indicator->indicator , ' ';
	} 
}
?>
</div>
</div>
<div class="row">
<div class="span2">
Attendance 
</div>
<div class="span7">
<?php
foreach ($student->attendances->attendances as $attend) {
	if ($attend_map[$attend->event]) {
		array_push($attend_map[$attend->event], $attend->date);
	} else {
		$attend_map[$attend->event] = array($attend->date);
	}
}
foreach (array_keys($attend_map) as $key) {
	if ($key == 'In Attendance') {
		$style = ' class="text-success"';
	} elseif ($key == 'Unexcused Absence') {
		$style = ' class="text-error"';
	} else {
		$style = ' class="text-warning"';
	}
	sort($attend_map[$key]);
	echo '<abbr title="' . $key . ': ' . implode(', ', $attend_map[$key]) . '"' . $style . '>' . count($attend_map[$key]) . '</abbr> ';
}
?>	
</div>
</div>
<pre>
<?php
	echo 'Grades: ';
	var_dump($student->studentGradebookEntries);
	echo '<br>';
	echo '<br>';
	echo 'Transcript: ';
	var_dump($student->transcript);
	echo '<br>';
	echo '<br>';
	var_dump($student);
?>
</pre>
</div>
</div>
  </body>
</html>

